﻿/*--------------------------------------------------------------------------------------+
|
|  $Source: PowerProduct/OpenRoadsDesignerSDK/ORDExamples/ManagedSDKExample/Examples/PickAlignmentTool.cs $
|
|  $Copyright: (c) 2018 Bentley Systems, Incorporated. All rights reserved. $
|
+--------------------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Bentley.GeometryNET;
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.MstnPlatformNET;
using System.Windows.Forms;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.SDK.Edit;
using Bentley.CifNET.SDK;
using Bentley.CifNET.LinearGeometry;
using CheccCoverplateCulvertAssemble.Tools;
using CheccCoverplateCulvertAssemble.Common;

namespace WaterDropIsland.util
{
    class PickAlignmentCommand : DgnElementSetTool
    {
        Alignment al_中线 = null;
        List<Alignment> als = new List<Alignment>();
        DPoint3d top;

        public PickAlignmentCommand() : base()
        {
        }

        protected override void OnRestartTool()
        {
            InstallNewInstance();
        }

        protected override void ExitTool()
        {
            base.ExitTool();
        }


        protected override void OnPostInstall()
        {
            BeginDynamics();
        }


        protected override bool OnDataButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
        {
            Bentley.DgnPlatformNET.HitPath hitPath = DoLocate(ev, true, 1);
            if (hitPath == null)
                return false;

            Element el = hitPath.GetCursorElement();
            if (el == null)
                return false;
            ConsensusConnectionEdit con = ConsensusConnectionEdit.GetActive();
            Alignment al = (el.ParentElement == null) ? Alignment.CreateFromElement(con, el) : Alignment.CreateFromElement(con, el.ParentElement);

            if (al == null)
                return false;

            if (al_中线 == null)
            {
                al_中线 = al;
                top = GetMeterPoint(ev.Point);
                NotificationManager.OutputPrompt("请按顺序选择边线");
            }
            else als.Add(al);

            if (als.Count == 3)
            {
                string GLLevel = ElementProperty.getElePropertyValue(al_中线.DgnECInstance.Element, "公路工程", "路线", "公路等级");
                string GLSS = ElementProperty.getElePropertyValue(al_中线.DgnECInstance.Element, "公路工程", "路线", "设计时速(km/h)");
                string GL路基宽 = ElementProperty.getElePropertyValue(al_中线.DgnECInstance.Element, "公路工程", "路线", "路基宽度(米)");

                //TemplateStandardLibrary.src.Tools.Database.ProcessDB processDB = new TemplateStandardLibrary.src.Tools.Database.ProcessDB();
                //processDB.DbInfoToDataTableCall_xhy();

                //TemplateStandardLibrary.src.Tools.Database.formData data = new TemplateStandardLibrary.src.Tools.Database.formData();
                //string str左侧中央带 = data.get左侧中央带_XHyu(GLLevel, GLSS, GL路基宽);

                //main.Keyin.islandForm.str左侧中央带 = str左侧中央带;
                //main.Keyin.islandForm.alignment = al_中线;
                //main.Keyin.islandForm.alignments = als;
                //main.Keyin.islandForm.top = top;

                //service.CreateIsland create = new service.CreateIsland();

                //create.AssignmentAlignment(al_中线, als, top, str左侧中央带);

                ExitTool();
            }


            return true;
        }

        protected override void OnDynamicFrame(DgnButtonEvent ev)
        {

        }

        public override Bentley.DgnPlatformNET.StatusInt OnElementModify(Bentley.DgnPlatformNET.Elements.Element element)
        {
            return Bentley.DgnPlatformNET.StatusInt.Error;
        }

        protected override bool OnResetButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
        {
            //main.Keyin.islandForm.alignment = null;
            //main.Keyin.islandForm.alignments = new List<Alignment>();

            ExitTool();
            return true;
        }
        public static void InstallNewInstance()
        {
            //开启“精确捕捉”的状态。
            Bentley.DgnPlatformNET.AccuSnap.SnapEnabled = true;
            //开启“精确捕捉”的自动定位功能。
            Bentley.DgnPlatformNET.AccuSnap.LocateEnabled = true;
            //设置捕捉方式为最近点
            Bentley.MstnPlatformNET.Settings.SnapMode = SnapMode.Nearest;
            //放置一个点
            NotificationManager.OutputPrompt("选择主路线");
            PickAlignmentCommand tool = new PickAlignmentCommand();
            tool.InstallTool();
        }
        public static DPoint3d GetMeterPoint(DPoint3d uorpoint)
        {
            double u = UorPerMeter();

            return DPoint3d.FromXYZ(
                uorpoint.X / u,
                uorpoint.Y / u,
                uorpoint.Z / u);

        }
        public static double UorPerMeter()
        {
            ModelInfo modelInfo = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel().GetModelInfo();
            double uorsPerMeter = modelInfo.UorPerMeter;
            return uorsPerMeter;
        }
        private DPoint3d PointOfIntersect(Element ele1, Element ele2)
        {
            CurveVector cveVcr1 = CurvePathQuery.ElementToCurveVector(ele1);
            CurveVector cveVcr2 = CurvePathQuery.ElementToCurveVector(ele2);
            CurveVector cveVcrInter1 = CurveVector.Create(CurveVector.BoundaryType.None), cveVcrInter2 = CurveVector.Create(CurveVector.BoundaryType.None);
            CurveCurve.IntersectionsXY(cveVcrInter1, cveVcrInter2, cveVcr1, cveVcr2);
            DPoint3d pt;
            cveVcrInter1.GetStartPoint(out pt);
            return pt;
        }

        private void 返回路基宽度数组(string file路基宽度, out double[] W)
        {
            List<double> wList = new List<double>();
            try
            {
                System.IO.StreamReader file1 = new System.IO.StreamReader(file路基宽度);
                string[] Rtxt = null;
                //读取标题行
                string txt = file1.ReadLine();
                //读取数据行
                while ((txt = file1.ReadLine()) != null)
                {
                    if (txt != "")
                    {
                        Rtxt = txt.Split(',');
                    }
                }
                file1.Close();
                //路基数据文件: \控制参数\公路_路基宽度_高速公路_80_25.5.txt
                //公路等级0,设计时速1,路基总宽2,左侧土路肩3,左侧硬路肩4,左侧行车道5,左侧路缘带6,左侧中央带7,右侧中央带8,右侧路缘带9,右侧行车道10,右侧硬路肩11,右侧土路肩12,单个行车道13,外侧路缘带14
                //高速公路,80,25.5,0.75,3,7.5,0.5,1,1,0.5,7.5,3,0.75,3.75,0.5
                for (int i = 2; i < Rtxt.Length; i++)
                {
                    wList.Add(Convert.ToDouble(Rtxt[i])); //路基总宽0,左侧土路肩1,左侧硬路肩2,左侧行车道3,左侧路缘带4,左侧中央带5,右侧中央带6,右侧路缘带7,右侧行车道8,右侧硬路肩9,右侧土路肩10,单个行车道11,外侧路缘带12
                }
                W = wList.ToArray();
            }
            catch (Exception)
            {
                W = null;
            }
        }

    }
}
